//
//  BaseViewControler.m
//  Weike
//
//  Created by leirz on 14-6-3.
//  Copyright (c) 2014年 winupon. All rights reserved.
//
#import "BaseViewController.h"
#import "AppUtils.h"
//#import "UMMobClick/MobClick.h"
#import "UIViewController+Alert.h"
#import "MainViewController.h"
#import "DDMenuController.h"

@interface BaseViewController() <UINavigationControllerDelegate, UITextFieldDelegate, UITextViewDelegate>

@property (nonatomic) CGFloat keyBoardHeight;
@property (nonatomic) CGFloat keyBoardDuration;

@property (nonatomic,strong) UIView *customBtnView;
@property (nonatomic,strong) UIView *customFaceView;

@property (nonatomic,strong) UIView *currentTextField;//当前的选中的文本域

@end


@implementation BaseViewController

@synthesize isPopControler;


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hasKeyBoardShow = YES;
    _keyBoardHeight = 270;
    _needSystemTitleView = NO;
    [AppUtils setBackButtonStyle:self];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSInteger count = self.navigationController.viewControllers.count;
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.delegate = self;
    //设置右侧滑的
    BOOL needDDMenu = NO;
    if(count == 1){
        UIViewController *last = self.navigationController.viewControllers.lastObject;
        if([last isKindOfClass:[MainViewController class]]){
            if( [((MainViewController *)last) getCurrentTabBarItemType] != TabBarItemTypeSupervise){
                needDDMenu = YES;
            }
        }
    }
    [[AppUtils getDDMenuViewController] addPanGesture:needDDMenu ];
    
    [AppUtils showStatusBar:YES needAnimation:NO];
    [self registerNotification];
//    [MobClick beginLogPageView:[self description]];
}


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self hideInput];
//    [MobClick endLogPageView:[self description]];
    isPopControler = [self.navigationController.viewControllers indexOfObject:self] == NSNotFound;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

//自定义表情相关--start
- (void)dealloc
{
    [self unRegisterNotifaction];
}

- (void)registerNotification
{
    [self unRegisterNotifaction];
    [AppUtils addObserver:self selector:@selector(onkeyBoardChange1:) name:UIKeyboardWillShowNotification];
    [AppUtils addObserver:self selector:@selector(onkeyBoardChange2:) name:UIKeyboardWillHideNotification];
    [AppUtils addObserver:self selector:@selector(onkeyBoardChange3:) name:UIKeyboardDidChangeFrameNotification];
    [AppUtils addObserver:self selector:@selector(onTextFieldDidChange:) name:UITextFieldTextDidChangeNotification];
    }

- (void)unRegisterNotifaction
{
    [AppUtils removeObserver:self name:UIApplicationWillResignActiveNotification];
    [AppUtils removeObserver:self name:UIKeyboardWillShowNotification];
    [AppUtils removeObserver:self name:UIKeyboardWillHideNotification];
    [AppUtils removeObserver:self name:UIKeyboardDidChangeFrameNotification];
    [AppUtils removeObserver:self name:UITextFieldTextDidChangeNotification];
}

-(void)setHasKeyBoardShow:(BOOL)hasKeyBoardShow1
{
    _hasKeyBoardShow = hasKeyBoardShow1;
    if(hasKeyBoardShow1){
        [self registerNotification];
    }
}


//切换标签和键盘
-(void)changeToFace
{
    [self hideInput];
    if(_customFaceView.hidden){ //打开表情,隐藏键盘
        
        
    }else{ //打开键盘 隐藏表情
        
        
    }
}

- (void)onkeyBoardChange1:(NSNotification *)notification
{
    [self onkeyBoardChange:notification];
}

- (void)onkeyBoardChange2:(NSNotification *)notification
{
    [self onkeyBoardChange:notification];
}

- (void)onkeyBoardChange3:(NSNotification *)notification
{
}
- (void)onTextFieldDidChange:(NSNotification *)notification
{
    
    UITextField *field =  notification.object;
    [self textFieldDidChange:field] ;
}


//键盘变化通知,子类复写方法即可
-(void)onkeyBoardChange:(NSNotification *)notification
{
    CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    _keyBoardDuration= [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    _keyBoardHeight = keyboardRect.size.height;
}

//复写 导航栏上的标题
- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    
    if(!_needSystemTitleView){
        [AppUtils setCustomTitleView:title];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

/**
 *  判断当前页面是否可见
 *
 *  @return
 */
- (BOOL)isVisible
{
    return self.view.window != nil;
}

- (void)destory
{
}

- (void)requestData
{
}

- (NSString *)description
{
    return [AppUtils getClassName:self];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

//----------------- 处理键盘 start -----------------
- (UIView *)frameRootView
{
    // 自定义键盘的需要在xib的self.view下另建1个外框frameRootView，然后移动这个frameRootView，否则键盘也会跟着self.view移动
    return nil;
}

- (void)moveUpFrameView:(UIView *)toView
{
    CGPoint ptText = [toView convertPoint:CGPointMake(0, toView.frame.size.height) toView:self.view];
    CGFloat textY = ptText.y;
    CGFloat bottomY = self.view.frame.size.height - textY;  // 输入框下面剩下的高度
    CGFloat keyboardH = _keyBoardHeight+20;//300; 20作为余量
    _customBtnView.bottom = SCREEN_HEIGHT-_keyBoardHeight;
    // 判断当前的高度是否已经够，如果超过了就不需要再移动主界面的View高度
    if (bottomY >= keyboardH) {
        return;
    }
    
    UIView *moveView = [self frameRootView] == nil ? self.view : [self frameRootView];
    CGFloat moveY = keyboardH - bottomY;
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
        moveView.top -= moveY;
        _customBtnView.bottom = SCREEN_HEIGHT-_keyBoardHeight+moveY;
    } completion:nil];
}

- (void)moveDownFrameView
{
    UIView *moveView = [self frameRootView] == nil ? self.view : [self frameRootView];
    CGFloat toTop = 0;
    if ([self frameRootView] == nil) {
        BOOL barHidden = [self.navigationController isNavigationBarHidden];
        toTop= barHidden ? 0 : 64;
    }
    
    [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
        moveView.top = toTop;
        _customBtnView.bottom = SCREEN_HEIGHT;
    } completion:nil];
}

- (void)hideInput
{
    [self.view endEditing:YES];
}

//------------------ textfield ----------------------
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self moveUpFrameView:textField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self hideInput];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self moveDownFrameView];
    [self hideInput];
}

- (void)textFieldDidChange:(UITextField *)textField
{
}

//------------------ textview ----------------------
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self moveUpFrameView:textView];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    [self moveDownFrameView];
    [self hideInput];
}

//----------------- 处理键盘 end -----------------


//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
//{
//    return NO;
//}
//
//- (BOOL)shouldAutorotate
//{
//    return NO;
//}
//
//- (UIInterfaceOrientationMask)supportedInterfaceOrientations
//{
//    return UIInterfaceOrientationMaskPortrait;
//}

@end


